<?php
// 配置类型模型
class StudentModel extends CommonModel {	
	
    protected $_validate = array(
    	   array('XS_XH','','学号已经存在！',0,'unique',1), // 在新增的时候验证字段是否唯一
//         array('XS_XM','require','姓名不能为空'),
//         array('XS_RXRQ','require','入学日期不能为空'),
//         array('XS_XX','require','就读学校不能为空'),
//         array('XS_NJ','require','就读年级不能为空'),
//         array('XS_BJ','require','就读班级不能为空'),
//         array('XS_JTZZ','require','家庭住址不能为空'),
//         array('XS_JTDH','require','家庭电话不能为空'),
//         array('XS_JZXM','require','家长姓名不能为空'),
//         array('XS_JZDH','require','家长电话不能为空'),
//         array('XS_JZMAIL','require','家长Email不能为空'),
    );

    protected $_auto =	array(
           	array('XS_XH','getStuXH',1,'callback'),
    		array('XS_PY','getStuPY',1,'callback'),
    		array('XS_PYSX','getStuPYSX',1,'callback'),
    		array('XS_XH','getStuXH',1,'callback'),
    		array('XS_XQ','getXQ',1,'callback'),
    		array('XS_FZR','getFZR',1,'callback'),
    		array('XS_ZT','xszt1'),  // 新增的时候把status字段设置为1 活跃
        //array('create_time','time',self::MODEL_INSERT,'function'),
    );
    
    //自动生成学号
    public function getStuXH(){  	
    	//$XS_XQ = $_POST['XS_XQ'];
        $XS_XQ = "HYXQ";
    	if(isset($_SESSION['xqid'])) {
    		$XS_XQ =$_SESSION['xqid'];
    	}
    	
    	$XS_RXRQ = $_POST['XS_RXRQ'];
    	//$XS_RXRQ = substr($XS_RXRQ, 0, 4);
    	$XS_XH = $XS_XQ . $XS_RXRQ  ;   	
		$map['XS_XH'] = array('like',"%".$XS_XH."%");
		//$map['XS_XH'] = $XS_XH;
		$Student = M ( "Student" );		
		$schoolList = array();		
		$schoolList = $Student->where($map)->limit (0,1)->order ( 'XS_XH desc' )->select();
		if(count($schoolList)!=0){
			$stu = $schoolList[0];
			$maxXH = $stu['XS_XH'];
			$lastNum = intval(substr($maxXH,-5));//后5位流水号
			$lastNum += 1;
			if($lastNum<10){
				$XS_XH = $XS_XH.'0000'.$lastNum;
			}else if($lastNum<100){
				$XS_XH = $XS_XH.'000'.$lastNum;
			}else if($lastNum<1000){
				$XS_XH = $XS_XH.'00'.$lastNum;
			}else{
				$XS_XH = $XS_XH.$lastNum;
			}					
		}else{
			$XS_XH = $XS_XH . '00001' ;
		}    	
    	return  $XS_XH;
    }
    
    //根据学生中文姓名生成拼音
    public function getStuPY(){
    	import('@.ORG.PinYin');
    	$XS_XM = $_POST['XS_XM'];
     	$PYUtil = new PinYin();
     	$XS_PY = $PYUtil->toPingyin($XS_XM);
    	return $XS_PY;
    }
    
    
    //根据学生中文姓名生成拼音(带参数 姓名,用户导入学生数据时候)
    public function getpyForImport($XS_XM){
    	import('@.ORG.PinYin');
    	$PYUtil = new PinYin();
    	$XS_PY = $PYUtil->toPingyin($XS_XM);
    	return $XS_PY;
    }

    //根据学生中文姓名生成拼音缩写
    public function getStuPYSX(){
    	import('@.ORG.FirstCharPinYin');
    	$XS_XM = $_POST['XS_XM'];
    	$PYUtil = new FirstCharPinYin();
    	$XS_PY = $PYUtil->conv($XS_XM);
    	return $XS_PY;
    }
    
    //根据学生中文姓名生成拼音(带参数 姓名,用户导入学生数据时候)
    public function getpysxForImport($XS_XM){
    	import('@.ORG.FirstCharPinYin');
    	$PYUtil = new FirstCharPinYin();
    	$XS_PY = $PYUtil->conv($XS_XM);
    	return $XS_PY;
    }
    
    //自动生成学号(带参数 校区和入学年份,用户导入学生数据时候)
    public function getXhForImport($XS_XQ,$XS_RXRQ){
    	$XS_XH = $XS_XQ . $XS_RXRQ  ;
    	$map['XS_XH'] = array('like',"%".$XS_XH."%");
    	$Student = M ( "Student" );
    	$schoolList = array();
    	$schoolList = $Student->where($map)->limit (0,1)->order ( 'XS_XH desc' )->select();
    	if(count($schoolList)!=0){
    		$stu = $schoolList[0];
    		$maxXH = $stu['XS_XH'];   		
    		$lastNum = intval(substr($maxXH,-5));//后5位流水号
			$lastNum += 1;
			if($lastNum<10){
				$XS_XH = $XS_XH.'0000'.$lastNum;
			}else if($lastNum<100){
				$XS_XH = $XS_XH.'000'.$lastNum;
			}else if($lastNum<1000){
				$XS_XH = $XS_XH.'00'.$lastNum;
			}else{
				$XS_XH = $XS_XH.$lastNum;
			}
    	}else{
    		$XS_XH = $XS_XH . '00001' ;
    	}
    	return  $XS_XH;
    }
    
    //根据中文性别判断性别标示
    public function getXbImport($XS_XB){
    	$xb = "O"; //保密
    	if($XS_XB == "男"){
    		$xb = "M";
    	} else if($XS_XB == "女"){
    		$xb = "F";
    	}
    	return $xb;
    }
    
    //获取学生入学日期
    public function getRxnfForImport($XS_NJ){
    	$nowDate = date("Y-m-d");
    	$nowDate = substr($nowDate, 0, 4);
    	$rxrq = $nowDate - $XS_NJ;
    	return $rxrq;
    }
    
    
    //获取
    public function getXQ(){
    	$dd_xq = "HKXQ";
    	if(isset($_SESSION['xqid'])) {
    		$dd_xq =$_SESSION['xqid'];
    	}
    	return $dd_xq;
    }
    
    public function getFZR(){
    	$xs_fzr = "班主任";
    	if(isset($_SESSION['loginUserName'])) {
    		$xs_fzr = $_SESSION['loginUserName'];
    	}
    	return $xs_fzr;
    }
    
    //获取学生信息列表
    public function getModeDisplayList($offset,$rows){    
    	$result = array();
    	$where = " 1 = 1" ;
    	$sql = "select t.*,t1.ITEM_VALUE as XX,t2.ITEM_VALUE AS NJ,t3.ITEM_VALUE AS BJ 
					from  (select * from ".$this->tablePrefix."student  ORDER by id desc) as t
					join ".$this->tablePrefix."dictionary_list t1 on t.xs_xx = t1.ITEM_KEY
					join ".$this->tablePrefix."dictionary_list t2 on t.xs_nj = t2.ITEM_KEY
					join ".$this->tablePrefix."dictionary_list t3 on t.xs_bj = t3.ITEM_KEY ";
    
    	$arrNum = $this->db->query("select count(*) as num from ".$this->tablePrefix."student ");
    	$row = $arrNum[0]['num'];
    
    	$result["total"] = $row;
    	$rs = $this->db->query($sql . " limit $offset,$rows");
//     	$items = array();
//     	while($row = mysql_fetch_object($rs)){
//     		array_push($items, $row);
//     	}
    	$result["rows"] = $rs;
    
    	return $result;
    }
    
    public function getCourseStudentList($kcid,$offset,$rows){
    	
    	
    	$sql = "select * from ".$this->tablePrefix."student where XS_XH in(SELECT DD_KHBH
    		FROM ".$this->tablePrefix."order where DD_BH IN(select LS_DDBH from ".$this->tablePrefix."order_list where LS_KCBH=". $kcid . "))";
    	
    	$sqlCount = "select count(*) as num from ".$this->tablePrefix."student where XS_XH in(SELECT DD_KHBH
    		FROM ".$this->tablePrefix."order where DD_BH IN(select LS_DDBH from ".$this->tablePrefix."order_list where LS_KCBH=". $kcid . "))";
    	
    	$arrNum = $this->db->query($sqlCount);
    	$row = $arrNum[0]['num'];
    	
    	$result["total"] = $row;
    	$rs = $this->db->query($sql . " limit $offset,$rows");
//     	$items = array();
//     	while($row = mysql_fetch_object($rs)){
//     		array_push($items, $row);
//     	}
    	$result["rows"] = $rs;
    	
    	return $result;
    	
    }
    
}